function mergeSort(arr) {
  if (arr.length <= 1) return arr
  let midIndex = Math.floor(arr.length / 2)
  let left = arr.slice(0, midIndex)
  let right = arr.slice(midIndex)
  return merge(mergeSort(left), mergeSort(right))
}
function merge(left, right) {
  let i = 0
  let j = 0
  let newArr = []
  while (i < left.length && j < right.length) {
    if (left[i] < right[j]) {
      newArr.push(left[i])
      i++
    } else {
      newArr.push(right[j])
      j++
    }
  }
  if (i < left.length) {
    newArr = [...newArr, ...left.slice(i)]
  }
  if (j < right.length) {

    newArr = [...newArr, ...right.slice(j)]
  }
  return newArr
}

function getKMin(arr, k) {
  return mergeSort(arr).slice(0, k ); 
}
let arr = [4, 5, 1, 6, 2, 7, 3, 8]
console.log(getKMin(arr, 4));